package com.education.basic.utils;


import com.education.basic.dao.DepartmentDao;
import com.education.basic.dao.StudentDao;
import com.education.basic.dao.TeacherDao;
import com.education.basic.entity.Student;
import com.education.basic.entity.Teacher;
import com.yunshangwoniu.core.utils.Logger;

public class CodeProvider
{
	private static String schoolCode = "14";
	private static String teacherPrefix = "2314";
	
	/**
	 * 院系code生成器
	 * @return
	 */
	public static String getDepartmentCode()
	{
		String lastCode = ((DepartmentDao)BeanUtils.getBean("departmentDao")).getLastCode();
		String returnCode = "";
		if(lastCode==null||"".equals(lastCode))
		{
			returnCode ="01";
		}else
		{
			char[] ch = lastCode.toCharArray();
			int num = Integer.parseInt(String.valueOf(ch[1]))+1;
			if(num<10)
			{
				returnCode = "0"+num;
			}else
			{
				returnCode = num+"";
			}
		}
		Logger.debug(CodeProvider.class, "from CodeProvider.class new departmentCode is"+returnCode);
		return returnCode;
	}
	
	public static String getStudentNo(Student s)
	{
		Student stu = ((StudentDao)BeanUtils.getBean("studentDao")).getByDepartCodeAndMajorCode(s.getDepartmentId()+"", s.getProfessionId()+"");
		String rank = "";
		if(stu==null)
		{
			rank = "01";
		}else
		{
			if(stu.getRank()<9)
			{
				rank = "0"+(stu.getRank()+1);
			}
		}
		long departmentId = s.getDepartmentId();
		long professionId = Long.parseLong(s.getProfessionId());
		String dep = "";
		String pro = "";
		String year = DateUtils.getCurrentYear();
		if(departmentId<10)
		{
			dep = "0"+departmentId;
		}else
		{
			dep = (departmentId)+"";
		}
		if(professionId<10)
		{
			pro = "0"+professionId;
		}else 
		{
			pro = professionId+"";
		}
		s.setRank(Integer.parseInt(rank));
		String stuNo = year+schoolCode+dep+pro+rank;
		Logger.debug(CodeProvider.class, String.format("generate stuNo success by CodeProvider [stuNo:%s]", stuNo));
		return  stuNo;
	}
	
	public static String getTeacherNo(Teacher t)
	{
		Teacher tea = ((TeacherDao)BeanUtils.getBean("teacherDao")).getByDepartmentIdAndTitleCode(t.getDepartmentId()+"", t.getTitleCode()+"");
		String rank = "";
		if(tea==null)
		{
			rank = "01";
		}else
		{
			if(tea.getRank()<9)
			{
				rank = "0"+(tea.getRank()+1);
			}
		}
		String dep = "";
		long departmentId = t.getDepartmentId();
		if(departmentId<10)
		{
			dep = "0"+departmentId;
		}else
		{
			dep = departmentId + "";
		}
		t.setRank(Integer.parseInt(rank));
		String teacherNo = teacherPrefix+dep+t.getTitleCode()+rank;
		Logger.debug(CodeProvider.class, String.format("generate teacher success by CodeProvider [teacherNo:%s]", teacherNo));
		return teacherNo;
	}
}
